<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
      <el-form-item label="岗位名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入岗位名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="岗位状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择岗位状态" clearable>
          <el-option v-for="item in options" :key="item" :label="item" :value="item">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button v-hasPermi="['system:post:list']" type="primary" icon="el-icon-search" size="small"
          @click="handleQuery">搜索
        </el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
        <el-button v-hasPermi="['system:post:add']" type="primary" plain icon="el-icon-plus" size="small"
          @click="handleAdd">新增
        </el-button>
        <el-button v-hasPermi="['system:post:delete']" type="danger" plain icon="el-icon-delete" size="small"
          :disabled="multiple" @click="handleDelete">
          批量删除
        </el-button>
        <el-button v-hasPermi="['system:post:export']" type="warning" plain icon="el-icon-download" size="small"
          @click="handleExport">导出
        </el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="岗位名称" align="center" prop="name" />
      <el-table-column label="岗位状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch active-value="启用" inactive-value="禁用" v-model="scope.row.status"
            @change="updatePostStatus(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="更新时间" align="center" prop="updateTime" />
      <el-table-column label="岗位备注" align="center" prop="remark" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermi="['system:post:update']" size="mini" type="text" icon="el-icon-edit"
            @click="handleUpdate(scope.row)">修改
          </el-button>
          <el-button v-hasPermi="['system:post:delete']" size="mini" type="text" icon="el-icon-delete"
            @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination style="float: right;margin-top: 20px;" @size-change="handleSizeChange"
      @current-change="handleCurrentChange" background :current-page="queryParams.currentPage"
      :page-sizes="[5,10,50,100]" :page-size="queryParams.pageSize" layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="岗位名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入岗位名称" />
        </el-form-item>
        <el-form-item label="岗位备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listPost,
    getPost,
    delPost,
    addPost,
    updatePost
  } from "@/api/system/post";

  export default {
    name: "Post",
    data() {
      return {
        loading: true,
        // 选中数组
        ids: [],
        // 非多个禁用
        multiple: true,
        // 总条数
        total: 0,
        // 岗位表格数据
        postList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        options: ["启用", "禁用"],
        // 查询参数
        queryParams: {
          currentPage: 1,
          pageSize: 10,
          name: undefined,
          status: undefined
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: {
            required: true,
            message: "岗位名称不能为空",
            trigger: "blur"
          }
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      updatePostStatus(row) {
        const text = row.status
        this.$confirm('确认要"' + text + '""' + row.name + '"岗位吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          updatePost(row).then(() => {
            this.getList()
            this.$message.success(text + '成功')
          }).catch(() => {
            this.getList()
          })
        }).catch(() => {
          this.getList()
        })
      },
      handleCurrentChange(val) {
        this.queryParams.currentPage = val;
        this.getList();
      },
      handleSizeChange(val) {
        this.queryParams.pageSize = val;
        this.getList();
      },
      /** 查询岗位列表 */
      getList() {
        this.loading = true
        listPost(this.queryParams).then(response => {
          this.postList = response.data.records;
          this.total = response.data.total;
          this.loading = false
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          name: undefined,
          remark: undefined
        };
        this.$refs['queryForm'].resetFields()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.currentPage = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.$refs['queryForm'].resetFields()
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加岗位";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        getPost(row.id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改岗位";
        });
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updatePost(this.form).then(response => {
                this.$message.success("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addPost(this.form).then(response => {
                this.$message.success("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        let ids = row.id || this.ids
        this.$confirm(`此操作删除id为【${ids}】的岗位, 是否继续?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delPost(ids).then(res => {
            this.getList();
            this.$message.success("删除成功");
          })
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('system/post/export', {
          ...this.queryParams
        }, '岗位表.xlsx')
      }
    }
  };
</script>
